#include "mygameobject.h"

//---------OVERRIDABLE METHODS--------------------------------------------

/***********************************
*
***********************************/
bool CMyGameObject::initialise(LJMUUtilShaderTexSheet* pshader, ID3D10Device* pdevice,std::wstring ptexfilename, int pspritewidth, int pspriteheight,int pnocells)
{
	this->_obj_shader = pshader;
	this->_obj_sprite = new LJMUUtilComplexSprite;
	this->_obj_sprite->initialise(pdevice,ptexfilename,pspritewidth,pspriteheight,pnocells);
	this->_no_frames = pnocells;

		
	return true;
}

/***********************************
*
***********************************/
bool CMyGameObject::update(float ptpf)
{
	//Update our Transforms as SRT Matrix Operations to get our World Matrix.
	D3DXMatrixTranslation(&_sprite_trans,this->_x_pos,this->_y_pos,DEFAULT_Z_POS);
	D3DXMatrixRotationZ(&_sprite_rot,D3DXToRadian(this->_angle));
	D3DXMatrixScaling(&_sprite_scale,this->_x_scale,this->_y_scale,DEFAULT_Z_SCALE);

	D3DXMatrixTranslation(&_rect_trans,this->_x_pos,this->_y_pos,DEFAULT_Z_POS);
	D3DXMatrixRotationZ(&_rect_rot,D3DXToRadian(this->_angle));
	D3DXMatrixScaling(&_rect_scale,this->_x_scale,this->_y_scale,DEFAULT_Z_SCALE);



	this->_sprite_world = this->_sprite_scale * this->_sprite_rot * this->_sprite_trans;
	this->_rect_world = this->_rect_scale * this->_rect_rot * this->_rect_trans;
	this->setRect();
	return true;
}

/***********************************
*
***********************************/
bool CMyGameObject::render(float ptpf,D3DXMATRIX pview, D3DXMATRIX portho)
{
	this->_obj_sprite->render();	
	this->_obj_shader->render(this->_obj_sprite->getIndexCount(),
		_sprite_world,pview,portho,this->_obj_sprite->getTexture(),getFrameUV());
	return true;
}

/***********************************
*
***********************************/
bool CMyGameObject::cleanup()
{
	this->_obj_sprite->cleanup();
	this->_obj_sprite = NULL;
	this->_obj_shader = NULL;
	return true;
}

//---------PUBLIC METHODS-------------------------------------------------


/***********************************
*
***********************************/
float CMyGameObject::getWidth()
{
	return this->_obj_sprite->getWidth();
}

/***********************************
*
***********************************/
float CMyGameObject::getHeight()
{
	return this->_obj_sprite->getHeight();
}

/***********************************
*
***********************************/
float CMyGameObject::getHalfWidth()
{
	return this->_obj_sprite->getHalfWidth();
}

/***********************************
*
***********************************/
float CMyGameObject::getHalfHeight()
{
	return this->_obj_sprite->getHalfHeight();
}

/***********************************
*
***********************************/
void CMyGameObject::setScale(float pscale)
{
	this->setXScale(pscale);
	this->setYScale(pscale);
}

/***********************************
*
***********************************/
void CMyGameObject::setRect()
{
	this->_rct_shape.left = this->getXPos() - this->getHalfWidth();
	this->_rct_shape.top = this->getYPos() - this->getHalfHeight();
	this->_rct_shape.right = this->getXPos() + this->getHalfWidth();
	this->_rct_shape.bottom = this->getYPos() + this->getHalfHeight();
}


/***********************************
*
***********************************/
void CMyGameObject::setPosition(float px,float py)
{
	this->setXPos(px);
	this->setYPos(py);
}	

/***********************************
*
***********************************/
void CMyGameObject::addScale(float pscale)
{
	this->setXScale(this->getXScale() + pscale);
	this->setYScale(this->getYScale() + pscale);
}

/***********************************
*
***********************************/
void CMyGameObject::addPosition(float px,float py)
{
	this->setXPos(this->getXPos() + px);
	this->setYPos(this->getYPos() + py);
}

/***********************************
*
***********************************/
void CMyGameObject::addRotation(float prot)
{
	this->setRotation(this->getRotation() + prot);
}

//---------INTERNAL METHODS-----------------------------------------------

/***********************************
*
***********************************/
void CMyGameObject::setDefaults()
{
	this->_x_pos = 0;
	this->_y_pos = 0;
	this->_angle = 0;
	this->_x_scale = 1;
	this->_y_scale = 1;
	this->_currentframe = 0;
	this->_active  = true;

	//Set all our Matrices to the Identity Matrix
	D3DXMatrixIdentity(&this->_sprite_rot);
	D3DXMatrixIdentity(&this->_sprite_trans);
	D3DXMatrixIdentity(&this->_sprite_scale);
	D3DXMatrixIdentity(&this->_sprite_world);
}

/***********************************
*
***********************************/
float CMyGameObject::getFrameUV()
{
	float tuv = (float)this->_currentframe / (float)this->_no_frames;
	return tuv;
}
